package com.dnjn.happiness.system.domain.vo;

import com.dnjn.happiness.system.api.domain.SysDept;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Treeselect树结构实体类
 *
 * @author happy
 */
@Getter
@Setter
public class TeamTreeSelect implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 节点ID
     */
    private Long id;

    /**
     * 节点名称
     */
    private String label;

    private String deptName;

    private String avatar;

    private String sex;

    private Long deptId;

    private List<UserNode> users;

    /**
     * 子节点
     */
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private List<TeamTreeSelect> children;


    @Setter
    @Getter
    public static class UserNode implements Serializable {
        private Long userId;
        private String userName;
        private String nickName;
        private String avatar;
        private String sex;
    }

    public TeamTreeSelect() {

    }

    public TeamTreeSelect(SysDept dept) {
        this.id = dept.getLeaderId();
        this.label = dept.getLeaderName();
        this.deptName = dept.getDeptName();
        this.deptId = dept.getDeptId();
        this.avatar = dept.getAvatar();
        this.sex = dept.getSex();
        this.children = dept.getChildren().stream().map(TeamTreeSelect::new).collect(Collectors.toList());
        List<UserNode> userNodeList = new ArrayList<>(dept.getUsers().size());
        dept.getUsers().forEach(user -> {
//            if (!user.getUserId().equals(dept.getLeaderId())) {
                UserNode userNode = new UserNode();
                BeanUtils.copyProperties(user, userNode);
                userNodeList.add(userNode);
//            }
        });
        this.users = userNodeList;
    }


}
